package net.googlecode.demenkov.services.impl;

import net.googlecode.demenkov.dao.MessageDAO;
import net.googlecode.demenkov.domains.Message;
import net.googlecode.demenkov.services.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.sql.Time;

/**
 * Service-class for actions with messages
 *
 * @author Demenkov Yura
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDAO messageDAO;

    @Transactional(readOnly = true)
    public Message getMessage(int messageId) {
        return messageDAO.get(messageId);
    }

    @Transactional
    public void createMessage(Message message) {
        message.setDate(new Date(System.currentTimeMillis()));
        message.setTime(new Time(System.currentTimeMillis()));
        messageDAO.create(message);
    }

    @Transactional
    public void deleteMessage(Message message) {
        messageDAO.delete(message);
    }

    @Transactional
    public void setReadMessage(Message message) {
        message.setRead(true);
        messageDAO.update(message);
    }
}
